﻿// Copyright (c) Inossem. All rights reserved.
// Licensed under the LGPL-3.0 license. See LICENSE file in the project root for full license information.
using DWF.Studio.Plugin.Contract;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using WPFLocalizeExtension.Engine;

namespace DWF.Studio.Plugin.ChromeExtensionInstaller
{
    public class PluginInstaller : PluginBase
    {
        public PluginInstaller(IPluginInfo pluginInfo)
           : base(pluginInfo)
        {
        }

        public override void Excute()
        {
            var workingDirectory = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Extension");
            var installer = Path.Combine(workingDirectory, "DWF.ProcessGoMsg.Installer.exe");
            string value = String.Empty;
            bool result;

            if (File.Exists(installer))
            {
                Process process = new Process();
                ProcessStartInfo processStartInfo = new ProcessStartInfo(installer);
                processStartInfo.WorkingDirectory = workingDirectory;
                processStartInfo.UseShellExecute = false;
                processStartInfo.RedirectStandardOutput = true;
                processStartInfo.Arguments = "\"chrome\"";
                process.StartInfo = processStartInfo;
                result = process.Start();

                StreamReader streamReader = process.StandardOutput;
                value = streamReader.ReadLine();
            }

            Boolean.TryParse(value, out result);

            string msg = String.Empty;
            if (result)
            {
                msg = GetLocalizedString("MsgBox_Msg_InstallChromeExtension_Success");
            }
            else
            {
                msg = GetLocalizedString("MsgBox_Msg_InstallChromeExtension_Fail");
            }

            MessageBox.Show(msg);
        }

        public override string GetLocalizedString(string key)
        {
            var result = key;
            var localdic = LocalizeDictionary.Instance;

            if (LocalizeDictionary.Instance.ResourceKeyExists(Assembly.GetExecutingAssembly().GetName().Name, "codelangdic", key, LocalizeDictionary.Instance.Culture))
            {
                result = LocalizeDictionary.Instance.GetLocalizedObject(Assembly.GetExecutingAssembly().GetName().Name, "codelangdic", key, LocalizeDictionary.Instance.Culture)?.ToString();
            }

            return result;
        }

        public override void Initialize(PluginParameters args)
        {

        }
    }
}
